/**
 * Created by 123 on 2017/12/26.
 */
import {queryTaskForm, taskSave} from '../../services/master'
import {Toast} from 'antd-mobile'
import {Actions} from 'react-native-router-flux';
import moment from 'moment';

function showToast(msg) {
  Toast.fail(msg, 2)
}

export default {
  namespace: 'masterTaskForm',

  state: {
    projectList: [],//工程列表
    nameList: [],//监督员列表
    currentProject: '',//选中的工程
    currentNameList: [],//选中的人
    taskName: '',//任务名称,
    hasChange: false,
    closingDate:'',//任务结束时间
    dialogText:'',
    taskText:'', //备注内容   remark
    prjname:'',
    hasDone:false,
      thisSelectOppen:false,
      thisBeSelectedLabel:'请选择项目',
      selectArrow:'horizontal'
  },

  effects: {
    *query({payload}, {call, put,select}){
      const {prjname} = yield select(state => state.masterTaskForm);
      const {res, error}= yield call(queryTaskForm,{prjname:prjname});

      if (error) {
        // showToast(error)
        yield put({type:'thisDialog/thisInit'});
        yield put({type:'dialogShow',payload:error});
      } else {
        console.log(res,'masterTaskForm')
        if (res.code == 1) {
          yield put({type: 'querySuccess', payload: res.data});
        } else {
          // showToast(res.message);
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:res.message});
        }

      }
    },

    *commit({payload}, {call, put, select}){
      yield put({type:'thisHasDone'});
      const {nameList, currentProject, taskName,closingDate,taskText}= yield select(state => state.masterTaskForm);
      const {userId}= yield select(state => state.login);

      let ids = [];
      for (let i = 0; i < nameList.length; i++) {
        if (nameList[i].isCheck) {
          ids.push({id: nameList[i].value});
        }
      }
      const param = {
        userId: userId,
        projectId: currentProject,
        // taskNum: taskNum,
        name: taskName,
        remark:taskText,
        taskUserList: ids,
        closingDate: moment(closingDate === ''?new Date():closingDate).format('YYYY-MM-DD'),
      };


      const {res, error}= yield call(taskSave,param);
      if (error) {
        yield put({type:'thisDialog/thisInit'});
        yield put({type:'dialogShow',payload:'提交错误，请重试！'});
        yield put({type:'thisDidNOtDone'});
      } else {
        if (res.code == 1) {
          yield put({type:'thisDidNOtDone'})
          yield put({type: 'masterTaskList/query'});
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'提交成功！'});
          Actions.pop();
        } else {
          yield put({type:'thisDidNOtDone'});
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:res.message});

        }
      }
    },
    //搜索
    *search({payload},{put,select,call}){
      yield put({type:'query'})
    },
  },
  reducers: {
    querySuccess(state, {payload}){

      let prjList = [];
      console.log('payload.prjName ==',payload.prjName)
      payload.prjName.map(val => {
        prjList.push({value: val.id, label: val.prjName});
      });
      let staffList = [];
      payload.name.map(val => {
        staffList.push({value: val.id, label: val.name, isCheck: false});
      });
      return {...state, projectList: prjList, nameList: staffList}

    },

    setCurrentProject(state, {payload}){

      return {...state, currentProject: payload}
    },

    setCurrentNameList(state, {payload}){
      let list = state.nameList;
      for (let i = 0; i < list.length; i++) {
        if (payload == list[i].value) {
          list[i].isCheck = !list[i].isCheck;
        }
      }

      return {...state, nameList: list, hasChange: !state.hasChange}
    },
    setTaskNum(state, {payload}){

      return {...state, taskNum: payload}
    },
    //备注内容
    setTaksText(state,{payload}){
      return {...state,taskText:payload}
    },
  //任务名称
    setTaskName(state, {payload}){
      return {...state, taskName: payload}
    },
    //搜索
    changeText(state,{payload}){
      return {...state,prjname:payload}
    },
    //提交thisHasDone/thisDidNOtDone
    thisHasDone(state,{payload}){
      return {...state,hasDone:true}
    },
    thisDidNOtDone(state,{payload}){
      return {...state,hasDone:false}
    },

    resetData(state, {payload}){
      return {
        ...state,
        // taskNum: '',//任务编号
        taskName: '',//任务名称
      }
    },
    //任务结束时间
    setClosingDate(state,{payload}){
      return{...state,closingDate:payload};
    },
    dialogShow(state,{payload}){
      return {...state,dialogText:payload}
    },
      getClean(state,{payload}){
          return {...state,
              projectList: [],
              nameList: [],
              currentProject: '',
              currentNameList: [],
              taskName: '',
              hasChange: false,
              closingDate:'',
              dialogText:'',
              taskText:'',
              prjname:'',
              hasDone:false,
              thisSelectOppen:false,
              thisBeSelectedLabel:'请选择项目',
              selectArrow:'horizontal'
          }
      },
      thisSelect(state,{payload}){
        let thisS = state.thisSelectOppen;
        let thisArrow = state.selectArrow;
        if (thisArrow === 'horizontal'){
            thisArrow = 'down';
        }else {
            thisArrow = 'horizontal'
        }
        thisS = !thisS;
        return {...state,thisSelectOppen:thisS,selectArrow:thisArrow}
      },
      thisBeSelected(state,{payload}){
          return {...state,thisBeSelectedLabel:payload.label,thisSelectOppen:false,selectArrow:'horizontal'}
      },
  }
}
